#include "platform.h"

PrynBaseLibraryEntryPoint (void *instance)
{
}

PrynResult prynGuiMenuCreatePopupPlatform (PrynGuiMenu *menu)
{
	PrynState *state = menu->pState;
	
	menu->pPlatform = CreatePopupMenu ();
	if (!menu->pPlatform) return PrynResultFailure;
	
	return PrynResultSuccess;
}

PrynResult prynGuiMenuCreateBarPlatform (PrynGuiMenu *menu)
{
	PrynState *state = menu->pState;
	
	menu->pPlatform = CreateMenu ();
	if (!menu->pPlatform) return PrynResultFailure;
	
	return PrynResultSuccess;
}

void prynGuiMenuDestroyPlatform (PrynGuiMenu *menu)
{
	if (menu && menu->pPlatform)
		DestroyMenu (menu->pPlatform);
	menu->pPlatform = 0;
}

PrynGuiExport (PrynResult, prynGuiMenuDisplay, (PrynGuiMenu *menu, int x, int y, PrynGuiControl *control, PrynGuiMenuItem *output))
{
	PrynStartFunction ();
	BOOL selected;

	if (output) *output = 0;
	prynAssert (control, NullArgument);
	state = control->pState;
	prynAssert (menu && menu->pPlatform && control->pPlatform, NullArgument);
	
	selected = TrackPopupMenu (menu->pPlatform,
		TPM_CENTERALIGN | TPM_VCENTERALIGN | TPM_RETURNCMD,
		x, y, 0,
		control->pPlatform, 0);
	if (output) *output = selected;
	prynAssert (selected, False);
	
	PrynEndFunction ();
}

PrynGuiExport (PrynResult, prynGuiMenuAppendItem, (PrynGuiMenu *menu, const PrynString *label, PrynGuiMenuData data, PrynGuiMenuItem *output))
{
	PrynStartFunction ();
	const char *labelz;
	PrynGuiMenuItem id;
	BOOL boolean;
	MENUITEMINFOA info;

	prynAssert (menu, NullArgument);
	state = menu->pState;
	prynAssert (label && label->pointer && menu->pPlatform, NullArgument);
	
	prynCall (prynStringToZ (label, menu->pState, &labelz));
	id = ++ menu->pItemCount;
	boolean = AppendMenuA (menu->pPlatform, MF_STRING | MF_ENABLED, id, labelz);
	if (labelz != label->pointer) prynFree (menu->pState, (void *) labelz);
	prynAssert (boolean, Failure);
	
	info.cbSize = sizeof (info);
	info.fMask = MIIM_DATA;
	info.dwItemData = data;
	prynAssert (SetMenuItemInfo (menu->pPlatform, id, FALSE, &info), Failure);

	if (output) *output = id;	
	PrynEndFunction ();
}

PrynGuiExport (PrynResult, prynGuiMenuItemData, (PrynGuiMenu *menu, PrynGuiMenuItem id, PrynGuiMenuData *output))
{
	PrynStartFunction ();
	MENUITEMINFOA info;
	
	if (output) *output = 0;
	prynAssert (menu, NullArgument);
	state = menu->pState;
	prynAssert (id, BadArgument);

	info.cbSize = sizeof (info);
	info.fMask = MIIM_DATA;
	prynAssert (GetMenuItemInfo (menu->pPlatform, id, FALSE, &info), Failure);

	if (output) *output = info.dwItemData;
	PrynEndFunction ();
}

